package com.cz.background.service.common;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.db.Page;
import com.cz.background.common.dto.PageResponseResult;
import com.cz.background.common.exception.BizException;
import com.cz.background.common.util.AuthUtil;
import com.cz.background.dto.common.user.*;
import com.cz.background.mapper.common.SysUserRoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import com.cz.background.mapper.common.SysUserMapper;
import com.cz.background.entity.common.SysUser;

import java.util.Date;
import java.util.List;

@Service
public class SysUserService{

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    public PageResponseResult<SysUser> queryUserPage(UserQueryDTO userQueryDTO){
        PageHelper.startPage(userQueryDTO.getPageNum(), userQueryDTO.getPageSize());
        List<SysUser> sysUsers = sysUserMapper.findByAll(userQueryDTO);
        PageInfo pageInfo = new PageInfo(sysUsers);
        PageResponseResult<SysUser> pageResponseResult = new PageResponseResult<>();
        pageResponseResult.setRecords(sysUsers);
        pageResponseResult.setPage(pageInfo.getPages());
        pageResponseResult.setTotal(pageInfo.getTotal());
        pageResponseResult.setPageSize(pageInfo.getPageSize());
        pageResponseResult.setPageNumber(pageInfo.getPageNum());
        return pageResponseResult;
    }

    public void addUser(@Valid AddUserDTO addUserDTO) {
        if(addUserDTO != null){
          SysUser sysUser =  BeanUtil.copyProperties(addUserDTO, SysUser.class);
          sysUser.setId(IdUtil.getSnowflakeNextIdStr());
          sysUserMapper.insert(sysUser);
        }
    }

    public void delUser(List<String> ids) {
        if(CollUtil.isEmpty(ids)){
            throw new BizException("500","参数不能为空");
        }
         sysUserMapper.deleteByIdIn(ids);
    }

    public void updateUserStatus(@Valid UpdateUserStatusDTO updateUserStatusDTO) {
        updateUserStatusDTO.setUpdateBy(AuthUtil.getUserName());
        sysUserMapper.updateStatus(updateUserStatusDTO);
    }

    public void updateUser(@Valid UpdateUserDTO updateUserDTO) {
       String id = updateUserDTO.getId();
       SysUser sysUser = sysUserMapper.selectByPrimaryKey(id);
       if(sysUser == null) {
           throw new BizException("500", "用户不存在");
       }
       BeanUtil.copyProperties(updateUserDTO, sysUser);
       sysUserMapper.updateByPrimaryKey(sysUser);
    }

    @Transactional
    public void assignRoles(AssignRoleDTO dto) {
        sysUserRoleMapper.deleteByUserId(dto.getUserId());
        if (dto.getRoleIds() != null && !dto.getRoleIds().isEmpty()) {
            sysUserRoleMapper.insertBatch(dto.getUserId(), dto.getRoleIds());
        }
    }
}
